﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Configuration;
using UFC.Common;

namespace TransferContainerStepRecord
{
    partial class TransferContainerStepRecord : ServiceBase
    {    
        //定时器
        private System.Timers.Timer m_tStepDispatch;//派工
        private System.Timers.Timer m_tStepWorkReport;//报工
        private System.Timers.Timer m_tStepCheck;//检验

        private System.Timers.Timer m_tStepNoUseDispatch;//无效派工
        private System.Timers.Timer m_tStepNoUseWorkReport;//无效报工
        private System.Timers.Timer m_tStepNoUseCheck;//无效检验


        private System.Timers.Timer m_tStepCheckPonint;//检测项特殊符号图片处理(转化base64)

        private System.Timers.Timer m_tStepNight;//夜晚处理项
        private System.Timers.Timer m_tStepRecordFix;//数据修复
        private System.Timers.Timer m_tSurpassBill;//超越单处理
        private System.Timers.Timer m_tApsClear;//排产清理定时器
        //

        TransferContainerStepRecordMethod runMain ;

        public TransferContainerStepRecord()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            // TODO: 在此处添加代码以启动服务。
            LogHelp.SetConfig();
            LogHelp.WriteLog("服务启动");
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());
            runMain = new TransferContainerStepRecordMethod();
          
            //派工线程
            SyncTimerStatr(ref m_tStepDispatch, m_tStepDispatch_Elapsed, intSync*40);
            //报工线程
            SyncTimerStatr(ref m_tStepWorkReport, m_tStepWorkReport_Elapsed, intSync * 40);
            //检验线程
            SyncTimerStatr(ref m_tStepCheck, m_tStepCheck_Elapsed, intSync * 40);
            //无效检验线程
            SyncTimerStatr(ref m_tStepNoUseCheck, m_tStepNoUseCheck_Elapsed, intSync * 60);
            //无效报工线程
            SyncTimerStatr(ref m_tStepNoUseWorkReport, m_tStepNoUseWorkReport_Elapsed, intSync * 60);
            //无效派工线程
            SyncTimerStatr(ref m_tStepNoUseDispatch, m_tStepNoUseDispatch_Elapsed, intSync * 60);
            //工序加工记录检查:批次删除、数量变更等需要自动修正情况
            SyncTimerStatr(ref m_tStepRecordFix, m_tStepRecordFix_Elapsed, intSync *3* 60);
            //检测项特殊符号图片
            SyncTimerStatr(ref m_tStepCheckPonint, m_tStepCheckPonint_Elapsed, intSync * 5 * 60);//5分钟
            //超越单处理
            SyncTimerStatr(ref m_tSurpassBill, m_tSurpassBill_Elapsed, intSync * 10 * 60);//10分钟
            //排产数据未下发清理
            SyncTimerStatr(ref m_tApsClear, m_tApsClear_Elapsed, intSync * 120 * 60);//2小时

            //闲时处理
            SyncTimerStatr(ref m_tStepNight, m_tStepNight_Elapsed, intSync*5*60);//5分钟
    
        }
        /// <summary>
        /// 传参启动新线程并运行方法
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="m_timerElapsed"></param>
        private void SyncTimerStatr(ref System.Timers.Timer timer, Action<object, System.Timers.ElapsedEventArgs> m_timerElapsed, int intSync)
        {
            timer = new System.Timers.Timer();
            timer.Enabled = false;
            timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timerElapsed);

            timer.Interval = intSync * 1000;
            timer.Enabled = true;
            timer.Start();
        }

        //排产历史数据清理
        private void m_tApsClear_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try {
                m_tApsClear.Enabled = false;
                runMain.ClearApsData();
            }
            catch (Exception ex) {
                LogHelp.WriteError("排产历史数据清理", ex);
            }
            finally {
                m_tApsClear.Enabled = true;
            }
        }
        //检测项特殊字符图片处理
        private void m_tStepCheckPonint_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tStepCheckPonint.Enabled = false;
                runMain.DeleteRepeat();
                runMain.CheckPointToBase64();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("检测项特殊字符图片", myError);
            }
            finally
            {
                m_tStepCheckPonint.Enabled = true;
            }
        }
        //派工
        private void m_tStepDispatch_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                m_tStepDispatch.Enabled = false;
                runMain.InsertDispachInfo();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("派工线程", myError);
            }
            finally
            {
                m_tStepDispatch.Enabled = true;
            }
        }
        //报工
        private void m_tStepWorkReport_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                m_tStepWorkReport.Enabled = false;
                runMain.UpdateWorkReport();
                runMain.InsertReportNoDispatch();
                runMain.DealRBReportStepRecord();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("报工线程", myError);
            }
            finally
            {
                m_tStepWorkReport.Enabled = true;
            }
        }
        //检验
        private void m_tStepCheck_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                m_tStepCheck.Enabled = false;
                runMain.UpdateCheckInfo();
                //runMain.UpdateCompleteInfoOfJgj();放在检验方法里
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("检验线程", myError);
            }
            finally
            {
                m_tStepCheck.Enabled = true;
            }
        }
        //无效检验
        private void m_tStepNoUseCheck_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                m_tStepNoUseCheck.Enabled = false;
                runMain.InValidCheck(false);
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("无效检验线程", myError);
            }
            finally
            {
                m_tStepNoUseCheck.Enabled = true;
            }
        }
        //无效报工
        private void m_tStepNoUseWorkReport_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                m_tStepNoUseWorkReport.Enabled = false;
                runMain.InValidReport(false);
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("无效报工线程", myError);
            }
            finally
            {
                m_tStepNoUseWorkReport.Enabled = true;
            }
        }
        //无效派工
        private void m_tStepNoUseDispatch_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                m_tStepNoUseDispatch.Enabled = false;
                runMain.InValidDispatch(false);
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("无效报工线程", myError);
            }
            finally
            {
                m_tStepNoUseDispatch.Enabled = true;
            }
        }
        //超越单处理
        private void m_tSurpassBill_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {

                m_tSurpassBill.Enabled = false;
                runMain.RunSurpassBill();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("超越单", myError);
            }
            finally
            {
                m_tSurpassBill.Enabled = true;
            }
        }

        //工序加工记录检查:批次删除、数量变更等需要自动修正情况
        private void m_tStepRecordFix_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tStepRecordFix.Enabled = false;
                runMain.RunStepRecordFix();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("工序加工检查", myError);
            }
            finally
            {
                m_tStepRecordFix.Enabled = true;
            }
        }

        //夜晚处理
        private void m_tStepNight_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                m_tStepNight.Enabled = false;
                var t = DateTime.Now;
                if (t.Hour > 1 && t.Hour < 5)
                {
                    runMain.ClearNightData();                    
                }
                //runMain.DealTeamEmp(); //测试 
                //runMain.DealEmpRes();//测试
            }
            catch (Exception myError)
            {
                LogHelp.WriteError("夜晚处理", myError);
            }
            finally
            {
                m_tStepNight.Enabled = true;
            }
        }
        
        protected override void OnStop()
        {
            // TODO: 在此处添加代码以执行停止服务所需的关闭操作。
        }
    }
}